package client.tzstore.oo.dialog.adapter

import android.content.Context
import android.content.Intent
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import client.comm.commlib.R
import client.comm.commlib.base.BaseAdapter
import client.comm.commlib.base.BindingViewHoder
import client.comm.commlib.comm_ui.bean.Areas
import client.comm.commlib.databinding.LayoutSelectshengItemBinding


class SelectShengAdapter : BaseAdapter<Areas.City> {

    var select = -1
    var selectId = ""

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val holder = BindingViewHoder(
            DataBindingUtil.inflate<ViewDataBinding>(
                layoutInflater,
                R.layout.layout_selectsheng_item, parent, false
            )
        )

        return holder
    }


    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val bindingViewHolder = holder as BindingViewHoder<LayoutSelectshengItemBinding>

        val binding = bindingViewHolder.binding
        val item = getItem(position) as Areas.City
        if (select == position) {
            binding.duiImg.visibility = View.VISIBLE
            binding.root.isSelected = true
        } else {
            binding.duiImg.visibility = View.GONE
            binding.root.isSelected = false
        }

        binding.name = item.name

        binding.root.setOnClickListener {
            val tmp = select
            select = position
            selectId = item.id
            notifyItemChanged(select)
            notifyItemChanged(tmp)
            event.invoke(item)
        }

    }

    constructor(context: Context) : super(context) {
    }

    var event: (city: Areas.City) -> Unit = {}


    fun setSelect(city: String?) {
        if (city == null) {
            select = -1
        } else {

            for ((c, key) in getItems().withIndex()) {
                if (key!!.name.trim().equals(city.trim())) {
                    select = c
                    selectId = key.id
                    notifyItemChanged(c)
                    break
                }
            }
        }


    }


}